global schedPerf;

%%
if ~exist('globalcntr_range', 'var')
    globalcntr_range = 1:100;
end

if ~exist('numTargets_range', 'var')
    numTargets_range = 20;
    %numTargets_range = [10 20 40 80];
end

if ~exist('beamwidth_range', 'var')
    beamwidth_range = deg2rad([1e-6 0.5 1 2 4 8]);
    %beamwidth_range = deg2rad(1);
end

if ~exist('method_range', 'var')
    %method_range = 1:7;
    %method_range = 1:4;
    %method_range = 6:7;
    %method_range = [3 7 8];
    %method_range = 8;
    %method_range = 9;
    %method_range = [1 2 6];
    %method_range = 10;
    %method_range = 3;
    %method_range = 11;
    %method_range = [7 12];
    %method_range = [7 11 12];
    %method_range = [3 9 13];
    method_range = [3 13];
end
  
%%
TSTART = tic;
%% Open logfile
filename = sprintf('logfile_%s.csv',datestr(now,'yyyymmddTHHMMSS_FFF'));
logfile = fopen(filename, 'at');

fprintf(logfile,'globalcntr, algorithm, numTargets, radarAngleResolution, num_radars, meanScanTime, meanTargetsVisited, delta_time, iterations \n');
fclose(logfile);

%%
param = Param_Init(0);

%%
for beamwidth = beamwidth_range
    for numTargets = numTargets_range
        
        for globalcntr = globalcntr_range
            
            param.radarAngleResolution = beamwidth;
            param.numTargets = numTargets;
            
            s = RandStream.create('mt19937ar','seed',globalcntr);
            RandStream.setDefaultStream(s);
            
            for method = method_range
                schedPerf.iterations = [];
                
                switch (method)
                    case 1
                        param.schedule.algorithm = 'IA';
                    case 2
                        param.schedule.algorithm = 'HR';
                    case 3
                        param.schedule.algorithm = 'GS'; %param.schedule.max_iterations = 12;
                    case 4
                        param.schedule.algorithm = 'HA';
                    case 5
                        param.schedule.algorithm = 'IH';
                    case 6
                        param.schedule.algorithm = 'IG';
                    case 7
                        param.schedule.algorithm = 'IS';
                    case 8
                        param.schedule.algorithm = 'G2';
                    case 9
                        param.schedule.algorithm = 'SA';
                    case 10
                        param.schedule.algorithm = 'SC';
                    case 11
                        param.schedule.algorithm = 'IM';
                    case 12
                        param.schedule.algorithm = 'IP';
                    case 13
                        param.schedule.algorithm = 'SG'; %param.schedule.max_iterations = 13;
                    otherwise
                        param.schedule.algorithm = 'HR';
                end
                
                reset(RandStream.getDefaultStream);
                
                fprintf(1,' -------- Run number %3d   %3d Targets BeamW %f  Method %s  -------------\n',globalcntr, param.numTargets, param.radarAngleResolution/pi*180, param.schedule.algorithm);
                
                Framework
                
                meanScanTimeMatrix(globalcntr,method) = mean(diff(visualiseState.time));
                
                meanTargetsVisitedMatrix(globalcntr,method) = mean(visualiseState.targetVisit);
                
                delta_time(globalcntr,method) = perfHist.elapsed_time;
                
                logfile = fopen(filename, 'at');
                fprintf(logfile,'%3d, %s, %3d, %f, %d, %f, %f, %f, %f \n',globalcntr, param.schedule.algorithm, param.numTargets, param.radarAngleResolution, param.num_radars, meanScanTimeMatrix(globalcntr,method), meanTargetsVisitedMatrix(globalcntr,method), delta_time(globalcntr,method), mean(schedPerf.iterations));
                fclose(logfile);
            end
        end
        
        figure; plot(meanScanTimeMatrix)
        title(strcat('Mean of Scan Times for ',num2str(param.numTargets),' Targets'))
        mean_meanScanTime = mean(meanScanTimeMatrix)
        std_meanScanTime = std(meanScanTimeMatrix)
        
        figure; plot(meanTargetsVisitedMatrix)
        title(strcat('Mean Number of Targets visited per scan for ',num2str(param.numTargets),' Targets'))
        mean_meanTargetsVisitedMatrix = mean(meanTargetsVisitedMatrix)
        std_meanTargetsVisitedMatrix = std(meanTargetsVisitedMatrix)
        
        
        %    Q_hist(globalcntr,:,:) = radarState.setup_select.Search.Q;
        
    end
end


%%
% Q(:,:) = mean(Q_hist);
% Q_std(:,:) = std(Q_hist);

toc(TSTART)
